/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.MessageComment;
import com.simon.mengkou.data.bean.base.MessageLike;
import com.simon.mengkou.data.bean.base.MessageNotice;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/11.
 * desc    : 消息-评论适配器
 */
public class MessageCommentAdapter extends AbsAdapter<MessageComment> {

    public MessageCommentAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_message_comment_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final MessageComment data = getItem(position);

        //头像
        holder.mSdvAvatar.loadAvatar(data.getUser().getAvatarUrl(), data.getUser().getLevel());
        //打开用户详情
        holder.mSdvAvatar.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                OuerDispatcher.presentUserActivity(mContext, data.getUser().getUserId(),
                        data.getUser().getNick());
            }
        });

        //标题
        holder.mTvTitle.setText(mContext.getString(R.string.message_comment_title, data.getUser().getNick()));
        //评论内容
        holder.mTvComment.setText(data.getContent());
        //时间
        holder.mTvTime.setText(UtilOuer.getFormatDate(mContext, data.getCreateAt(), "yy-M-d"));
        //图片,内容
        final Follow follow = data.getRelatePost();
        if(UtilList.isNotEmpty(follow.getImageArray())) {
            holder.mSdvImage.setVisibility(View.VISIBLE);
            holder.mTvContent.setVisibility(View.GONE);

            if(UtilList.isNotEmpty(data.getRelatePost().getImageArray())) {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage,
                        data.getRelatePost().getImageArray().get(0).getUrl());
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage,
                        data.getRelatePost().getVideoCover());
            }

            holder.mSdvImage.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    //打开晒单详情
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, follow.getId());
                }
            });
        } else if(UtilString.isNotBlank(follow.getContent())) {
            holder.mSdvImage.setVisibility(View.GONE);
            holder.mTvContent.setVisibility(View.VISIBLE);
            holder.mTvContent.setText(follow.getContent());

            holder.mTvContent.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    //打开晒单详情
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, follow.getId());
                }
            });
        } else {
            holder.mSdvImage.setVisibility(View.GONE);
            holder.mTvContent.setVisibility(View.GONE);
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //头像
        @Bind(R.id.follow_id_avatar)
        AvatarSimpleDraweeView mSdvAvatar;
        //标题
        @Bind(R.id.follow_id_name)
        TextView mTvTitle;
        //评论
        @Bind(R.id.follow_id_comment)
        TextView mTvComment;
        //内容
        @Bind(R.id.follow_id_content)
        TextView mTvContent;
        //时间
        @Bind(R.id.follow_id_time)
        TextView mTvTime;
        //图片
        @Bind(R.id.follow_id_img)
        SimpleDraweeView mSdvImage;
    }

}
